'use strict';
const dayjs = require("dayjs");
module.exports = app => {
    const { STRING, INTEGER } = app.Sequelize;
    const AdminUser = app.model.define(
        'AdminUser',
        {
            id: {type: INTEGER, primaryKey: true, autoIncrement: true},
            phone: STRING(255),
            username: STRING(255),
            nickname: STRING(255),
            password: STRING(255),
            avatar_url: STRING(255),
            status: INTEGER(11),
            role_id: INTEGER(11),
            created_time: {
                type: app.Sequelize.DATE,
                allowNull: false,
                get() {
                    return dayjs(this.getDataValue('created_time')).format('YYYY-MM-DD HH:mm:ss');
                },
            },
            updated_time: {
                type: app.Sequelize.DATE,
                allowNull: false,
                get() {
                    return dayjs(this.getDataValue('updated_time')).format('YYYY-MM-DD HH:mm:ss');
                },
            }
        },
        {
            tableName: 'user',
            timestamps: true, // 启用时间戳(createdAt, updatedAt)
            updatedAt: 'updated_time', // 不想要 updatedAt
            createdAt: 'created_time', // 想要  createdAt 但是希望名称叫做 created_at
        }
    );
    // 关联关系
    AdminUser.associate = function() {
        // 一对一角色
        app.model.AdminUser.belongsTo(app.model.Role, { foreignKey: 'role_id', targetKey: 'id' });
    }
    return AdminUser;
};
